package com.aier.user.action;

import java.util.List;

import javax.annotation.Resource;

import net.eicp.ghostboy.futil.MD5Util;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.classes.entity.Classes;
import com.aier.classes.service.IClassService;
import com.aier.common.common.CommonCookie;
import com.aier.common.common.CommonSession;
import com.aier.common.common.CookieCommon;
import com.aier.common.common.SystemConfigStatic;
import com.aier.school.entity.School;
import com.aier.school.service.ISchoolService;
import com.aier.user.entity.User;
import com.aier.user.service.IUserService;

@Scope("prototype")
@Component("userLoginIndexAction")
public class UserLoginIndexAction extends UserBaseAction{

    /**
     * 登录页面
     */
    private static final long serialVersionUID = 1L;

    @Resource(name = "userService")
    private IUserService userService;    
    
    @Resource(name = "schoolService")
    private ISchoolService schoolService;
    
    @Resource(name = "classService")
    private IClassService classService;    
    
    private int s;                    //成功:1 ,失败:0
    private String error_info;        //错误提示信息
    
    private String account;            //账号(手机号)
    private String pwd;                //密码
    private boolean rememberMe;        //记住密码
    private String phone_imei;        //手机唯一标示符
    private Long uid;                //用户id
    
    private User user;                    //用户
    private List<School> schoolList;    //用户所在学校
    private List<Classes> classList;    //用户所在班级
    
    
    @Override
    public String execute(){
        
        return SUCCESS;
    }
    
    /**
     * 登陆
     * @return
     */
    public String login(){
        s = 1;
        user = userService.searchUserByCondition(account, MD5Util.getMD5(pwd));
        if(user==null){
            s = 0;
            error_info = "用户名密码有误";
        }else{
            if(user.getIsbaby()==(short)1){
                s = 0;
                error_info = "用户名密码有误";
            }else{
                schoolList = schoolService.searchSchoolListByUid(user.getUid());
                classList = classService.searchClassListByUid(user.getUid());
                // 成功登录后记录session
                session.put(CommonSession.SESSION_ACCOUNT, user.getAccount());
                session.put(CommonSession.SESSION_ACCOUNT_ID, user.getUid());
                session.put(CommonSession.SESSION_ACCOUNT_NAME, user.getNickname());
                session.put(CommonSession.SESSION_ACCOUNT_SCHOOLLIST, schoolList);
                session.put(CommonSession.SESSION_ACCOUNT_CLASSLIST, classList);
                if (rememberMe) {//cookie
                    try {
                        CookieCommon.writeCookie(response,
                                 CommonCookie.COOKIE_KEY_REMEMBER_LOGIN,
                                 user.getAccount(), SystemConfigStatic.domain,
                                 Integer.MAX_VALUE);
                        CookieCommon.writeCookie(response,
                                 CommonCookie.COOKIE_KEY_PWD_LOGIN, user.getPwd(),
                                 SystemConfigStatic.domain, Integer.MAX_VALUE);
                    } catch (Exception e) {
                        s = 0;
                        error_info = "登陆成功，cookie写入失败";
                    }
                }                
            }    
        }
        
        return SUCCESS;
    }

    /**
     * 添加手机唯一标识
     */
    public String add_imei() {

        s = userService.updatePhoneImeiByUid(phone_imei, uid);

        return SUCCESS;
    }
    
    
    /**
     * 新版手机登录接口
     * @return
     */
    public String loginApp(){
        s = 1;
        user = userService.searchUserByCondition(account, MD5Util.getMD5(pwd));
        if(user==null){
            s = 0;
            error_info = "用户名密码有误";
        }else{
            if(user.getIsbaby()==(short)1){
                s = 0;
                error_info = "用户名密码有误";
            }else{
                schoolList = schoolService.searchSchoolListByUid(user.getUid());
                classList = classService.searchClassListByUid(user.getUid());
                
                
                                
            }    
        }
        
        return SUCCESS;
    }
    
    
    
    public int getS() {
        return s;
    }

    public String getError_info() {
        return error_info;
    }

    public User getUser() {
        return user;
    }

    public List<School> getSchoolList() {
        return schoolList;
    }

    public List<Classes> getClassList() {
        return classList;
    }

    public void setUserService(IUserService userService) {
        this.userService = userService;
    }

    public void setSchoolService(ISchoolService schoolService) {
        this.schoolService = schoolService;
    }

    public void setClassService(IClassService classService) {
        this.classService = classService;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public void setRememberMe(boolean rememberMe) {
        this.rememberMe = rememberMe;
    }

    public void setPhone_imei(String phone_imei) {
        this.phone_imei = phone_imei;
    }

    public void setUid(Long uid) {
        this.uid = uid;
    }
    
    
    
    
    
}
